package 力扣;

/**
 * @author yyq
 * @create 2022-03-08 10:53
 */
public class leetcode707 {
    public static void main(String[] args) {
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.addAtHead(0);

        linkedList.addAtIndex(1,4);
        linkedList.addAtTail(8);
        linkedList.addAtHead(5);
        linkedList.addAtIndex(4,3);
        linkedList.addAtTail(0);
        linkedList.addAtTail(5);
        linkedList.addAtIndex(6,3 );
        linkedList.deleteAtIndex(7);
        linkedList.deleteAtIndex(5);
        linkedList.addAtTail(4);

//        MyLinkedList linkedList = new MyLinkedList();
//        linkedList.addAtHead(1);
//        linkedList.addAtTail(3);
//        linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
//        linkedList.get(1);            //返回2
//        linkedList.deleteAtIndex(1);  //现在链表是1-> 3
//        linkedList.get(1);            //返回3


    }
}
class ListNode707{
    int val;
    ListNode707 pre;
    ListNode707 next;

    public ListNode707() {
    }

    public ListNode707(int val) {
        this.val = val;
    }

    public ListNode707(int val, ListNode707 pre, ListNode707 next) {
        this.val = val;
        this.pre = pre;
        this.next = next;
    }
}

class MyLinkedList {
    ListNode707 head=null;
    ListNode707 end=null;
    int length=0;
    public MyLinkedList() {
        head=new ListNode707();
        length=0;
    }

    public int get(int index) {
        // index从0开始
        if(index+1>length) return -1;
        ListNode707 temp=head.next;
        int count=0;
        while(temp!=null){
            if(count==index)
                return temp.val;
            else {
                temp=temp.next;
                count++;
            }
        }
        return -1;

    }

    public void addAtHead(int val) {
        // 如果为空链表
        if(head.next==null){
            head.next=new ListNode707(val,head,null);
            end=head.next;
            length++;
        }else {
            //不为空链表
            ListNode707 temp=head.next;
            ListNode707 one=new ListNode707(val,head,temp);
            head.next=one;
            temp.pre=one;
            length++;
        }

    }

    public void addAtTail(int val) {
        if(end!=null){
            // end不为null
            ListNode707 temp=new ListNode707(val);
            temp.pre=end;
            end.next=temp;
            end=temp;
            length++;
        }else {
            // end为null
            head.next=new ListNode707(val,head,null);
            end=head.next;
            length++;
        }



    }

    public void addAtIndex(int index, int val) {
        int count=0;
        if(index>length) return;
        if(index==length) {
            addAtTail(val);
            return;
        }
        if(index<=0) {
            addAtHead(val);
            return;
        }
        if(0<index&&index<length){
            ListNode707 temp=head.next;
            while (temp!=null){
                if(count==index){
                    // 忘记修改end的位置
                    if(temp.next!=end){
                        ListNode707 insert=new ListNode707(val);
                        ListNode707 pre=temp.pre;
                        insert.pre=pre;
                        pre.next=insert;
                        insert.next=temp;
                        temp.pre=insert;
                        length++;
                        return;
                    }else {
                        ListNode707 insert=new ListNode707(val);
                        ListNode707 pre=temp.pre;
                        insert.pre=pre;
                        pre.next=insert;
                        insert.next=temp;
                        temp.pre=insert;
                        end.pre=insert;
                        length++;
                        return;
                    }

                }
                temp=temp.next;
                count++;

            }
        }


    }

    public void deleteAtIndex(int index) {
        if(index<0||index+1>length) return;
        // 删除头节点
        if(index==0){
            // 只有一个节点
            if(length==1){
                end=end.pre;
                end.next=null;
                length--;
            }else {
                ListNode707 temp=head.next.next;
                head.next=temp;
                temp.pre=head;
                length--;
            }
            return;

        }
        if(index+1==length){
           end=end.pre;
           end.next=null;
           length--;
           return;
        }
        if(0<index&&index<length){
            ListNode707 temp=head.next;
            int count=0;
            while(temp!=null){
                if(count==index){
                    ListNode707 pre=temp.pre;
                    ListNode707 next=temp.next;
                    pre.next=next;
                    next.pre=pre;
                    length--;
                    return;
                }
                temp=temp.next;
                count++;
            }

        }


    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */
